package net.web.milkteapos.module;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletRequest;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.QueryResult;
import org.nutz.dao.pager.Pager;
import org.nutz.http.Request;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.lang.util.NutMap;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Fail;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import net.web.milkteapos.bean.Money;
import net.web.milkteapos.bean.Product;

@IocBean
@At("/product")
@Ok("json")
@Fail("http:500")
public class ProductModule {

	@Inject
	protected Dao dao;
	
	/**
	 * 新增
	 * @param product
	 * @return
	 */
	@At
	public Object add(@Param("..")Product product, @Param("..")Money money) {
		NutMap re = new NutMap();
        String msg = checkProduct(product, true);
        if (msg != null){
            return re.setv("ok", false).setv("msg", msg);
        }
        product.setCreateTime(new Date());   
        money.setCurrency("RMB");
        product.setMoney(money);
       
        dao.insertWith(product, "money");
        
        return re.setv("ok", true).setv("data", product);
	}
	
	/**
	 * 修改
	 * @param product
	 * @return
	 */
	@At
	public Object update(@Param("..")Product product, @Param("..")Money money) {
		NutMap re = new NutMap();
		
		System.out.println("名字："+product.getProductName());
        String msg = checkProduct(product, false);
        if (msg != null){
            return re.setv("ok", false).setv("msg", msg);
        }
        product.setCreateTime(new Date());
        money.setCurrency(null);
        product.setMoney(money);

        dao.updateWith(product, "money");
        return re.setv("ok", true);
	}
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@At
    public Object delete(@Param("id")int id) {
        if (id < 1) {
            return new NutMap().setv("ok", false).setv("msg", "商品ID非法");
        }
        Cnd cnd = Cnd.where("product_id", "=", id);
        Product product = dao.fetch(Product.class, cnd);
        dao.fetchLinks(product, "money");
        dao.deleteWith(product, "money");
        return new NutMap().setv("ok", true);
    }
	
	/**
	 * 查询
	 * @param name
	 * @param pager
	 * @return
	 */
	
	@At
    public Object query(@Param("name")String name, @Param("..")Pager pager) {
        Cnd cnd = Strings.isBlank(name)? null : Cnd.where("product_name", "like", "%"+name+"%");
        QueryResult qr = new QueryResult();
        qr.setList(dao.query(Product.class, cnd, pager));
        pager.setRecordCount(dao.count(Product.class, cnd));
        qr.setPager(pager);
        return qr; //默认分页是第1页,每页20条
    }
	
	@At
    public Object query_without_pager(@Param("name")String name) {
        Cnd cnd = Strings.isBlank(name)? null : Cnd.where("product_name", "like", "%"+name+"%");
        QueryResult qr = new QueryResult();
        List<Product> products = new ArrayList<>();
        List<Product> result = dao.query(Product.class, cnd);
        for (Product model : result) {
        
			dao.fetchLinks(model, "productType");
			dao.fetchLinks(model, "money");
			products.add(model);
		}
        qr.setList(products);
        return qr;
    }

	@At
    public Object query_full(@Param("id")int id) {
        Cnd cnd = Cnd.where("product_id", "=", id);
        Product product = dao.fetch(Product.class, cnd);
        dao.fetchLinks(product, "productType");
        dao.fetchLinks(product, "money");
        return product; //默认分页是第1页,每页20条
    }
	
	@At
    public int count() {
        return dao.count(Product.class);
    }
	
	private String checkProduct(Product product, boolean create) {
		if(product == null) {
			return "空对象";
		}
		if(Strings.isBlank(product.getProductName())&&product.getMoney()!=null) {
			return "商品信息不能为空";
		}
		
		if(!create && product.getProductId() < 1) {
			return "商品ID非法";
		}
		return null;
	}
}
